
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.net.Socket;

public class ProcessThread extends Thread {
   private Socket socket;
   private PrintWriter out ;
   private BufferedReader in ;
   static int t=0;
   int mt=0;

   public ProcessThread(Socket socket)
   {
      super("ProcessThread");
      this.socket = socket;

      try{
         out = new PrintWriter(socket.getOutputStream(), true);
         in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      }
      catch(IOException e){
         System.out.println("Can't create I/O.");
         e.printStackTrace();
      }
      mt = t++;
   }

   public void run()  {
      String fromServer;
      try{
         while ((fromServer = in.readLine()) != null)
         {
            System.out.println("(Thread "+ mt + " Server: " + fromServer);

            if (fromServer.equals("Bye."))
               break;

            out.println(fromServer);
         }
      }
      catch(IOException e){e.printStackTrace();}
   }
}
